package br.odb.liboldfart.parser.level;

import java.io.InputStream;
import java.util.ArrayList;

import br.odb.liboldfart.parser.FileFormatParser;
import br.odb.libscene.Actor;
import br.odb.libscene.Mesh;
import br.odb.libscene.Sector;
import br.odb.libscene.World;
import br.odb.libstrip.GeneralPolygon;
import br.odb.utils.FileServerDelegate;

public class GEOLoader implements FileFormatParser {
	
	private World world;
	
	public World getWorld() {
		return world;
	}
	
	
	public void parseDocument( FileServerDelegate fServe ) {
		
	}

	public void preBuffer( InputStream in ) {

		world = new World( in, new ArrayList<Actor>(), false );
	}

	public ArrayList< Mesh > getGeometry() {
		ArrayList<Mesh> toReturn = null;
		Mesh mesh = null;
		GeneralPolygon face = null;
		Sector sector;
		
		if ( world != null ) {
			toReturn = new ArrayList<Mesh>();
			
			for ( int c = 0; c < world.getTotalSectors(); ++c ) {
				mesh = new Mesh();
				toReturn.add( mesh );
				sector = world.getSector( c );
				mesh.setSolid( sector.isSolid() ); 
				//N
				face = new GeneralPolygon();
				mesh.addPoint( sector.getX0(), sector.getY0(), sector.getZ0() );
				mesh.addPoint( sector.getX1(), sector.getY0(), sector.getZ0() );
				mesh.addPoint( sector.getX1(), sector.getY1(), sector.getZ0() );
				mesh.addPoint( sector.getX0(), sector.getY1(), sector.getZ0() );
				face.addIndex( 0 );
				face.addIndex( 1 );
				face.addIndex( 2 );
				face.addIndex( 3 );
				//L
				face = new GeneralPolygon();
				mesh.addPoint( sector.getX1(), sector.getY0(), sector.getZ0() );
				mesh.addPoint( sector.getX1(), sector.getY0(), sector.getZ1() );
				mesh.addPoint( sector.getX1(), sector.getY1(), sector.getZ1() );
				mesh.addPoint( sector.getX1(), sector.getY1(), sector.getZ0() );
				face.addIndex( 0 );
				face.addIndex( 1 );
				face.addIndex( 2 );
				face.addIndex( 3 );
				//S
				face = new GeneralPolygon();
				mesh.addPoint( sector.getX0(), sector.getY0(), sector.getZ1() );
				mesh.addPoint( sector.getX1(), sector.getY0(), sector.getZ1() );
				mesh.addPoint( sector.getX1(), sector.getY1(), sector.getZ1() );
				mesh.addPoint( sector.getX0(), sector.getY1(), sector.getZ1() );
				face.addIndex( 0 );
				face.addIndex( 1 );
				face.addIndex( 2 );
				face.addIndex( 3 );
				//W
				face = new GeneralPolygon();
				mesh.addPoint( sector.getX0(), sector.getY0(), sector.getZ0() );
				mesh.addPoint( sector.getX0(), sector.getY0(), sector.getZ1() );
				mesh.addPoint( sector.getX0(), sector.getY1(), sector.getZ1() );
				mesh.addPoint( sector.getX0(), sector.getY1(), sector.getZ0() );
				face.addIndex( 0 );
				face.addIndex( 1 );
				face.addIndex( 2 );
				face.addIndex( 3 );
				//FLOOR
				face = new GeneralPolygon();
				mesh.addPoint( sector.getX0(), sector.getY0(), sector.getZ0() );
				mesh.addPoint( sector.getX1(), sector.getY0(), sector.getZ0() );
				mesh.addPoint( sector.getX1(), sector.getY0(), sector.getZ1() );
				mesh.addPoint( sector.getX0(), sector.getY0(), sector.getZ1() );
				face.addIndex( 0 );
				face.addIndex( 1 );
				face.addIndex( 2 );
				face.addIndex( 3 );
				//CEILING				
				face = new GeneralPolygon();
				mesh.addPoint( sector.getX0(), sector.getY1(), sector.getZ0() );
				mesh.addPoint( sector.getX1(), sector.getY1(), sector.getZ0() );
				mesh.addPoint( sector.getX1(), sector.getY1(), sector.getZ1() );
				mesh.addPoint( sector.getX0(), sector.getY1(), sector.getZ1() );
				face.addIndex( 0 );
				face.addIndex( 1 );
				face.addIndex( 2 );
				face.addIndex( 3 );
				
				mesh.addFace( face );
			}			
		}
		
		return toReturn;
	}
}
